package com.wondersgroup.fjzlpt.org.service.impl.fwb;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.quick.framework.exception.ServiceException;
import com.quick.framework.util.FormatDate;
import com.quick.framework.util.LoggerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.org.dao.inter.fwb.FwbDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.fwbFwxx.FwbFwxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.FwbgxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgxx.YljgxxDAO;
import com.wondersgroup.fjzlpt.org.service.inter.fwb.FwbService;
import org.springframework.util.StringUtils;


@Service
public class FwbServiceImpl implements FwbService{

	@Autowired
	private FwbDAO fwbDAO;
	
	@Autowired
	private FwbFwxxDAO fwbFwxxDAO;
	
	@Autowired
	private FwbgxDAO fwbgxDAO;
	
	@Autowired
	private YljgxxDAO yljgxxDAO;
	
	/**
	 * 分页查询服务包
	 * @param map
	 * @return
	 */
	//@Cacheable("queryPage")
	public JSONObject queryPage(Map<String, String> map) {
		return fwbDAO.queryPage(map);
	}

	/**
	 * 保存服务包信息
	 * @param map
	 * @return
	 */
	public JSONObject save(Map map) {
		ResultObject ro = new ResultObject();
		JSONObject fwbObj = fwbDAO.checkSaveName(map);
		if(!fwbObj.isEmpty()){
			ro.changeFaultState("存在相同的服务包名称，请重新输入！");
		}else{
			String method = map.get("method").toString();
			getXzqhdm(map);
			if(!StringUtils.isEmpty(map.get("CJSJ").toString())){
				map.put("CJSJ" , FormatDate.getDateY_M_D(map.get("CJSJ").toString()));
			}
			if("add".equals(method)){
				insert(map, ro);
			}
			else if("update".equals(method)){
				update(map, ro);
			}
		}
		return (JSONObject)JSON.toJSON(ro);
	}

	/**
	 * 获取行政区划代码
	 * @param map
	 */
	private void getXzqhdm(Map<String, String> map) {
		map.put("pageSize", "1");
		map.put("pageNumber", "1");
		JSONObject json = yljgxxDAO.queryPage(map); 
		json = (JSONObject) (json.getJSONArray("rows")).get(0);
		map.put("XZQHDM", json.getString("XZQHDM"));
	}
	
	/**
	 * 新增
	 * @param map
	 * @param ro
	 */
	private void insert(Map map, ResultObject ro){
		try {
			fwbDAO.insert(map);
			saveFwbFwxx(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000302","新增服务项失败！");
		}
	}
	
	/**
	 * 更新
	 * @param map
	 * @param ro
	 */
	private void update(Map map, ResultObject ro){
		try {
			fwbDAO.updateForPrimaryKey(map);
			if(map.containsKey("FWXS")){
				saveFwbFwxx(map);
			}
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000303","更新服务项失败");
		}
	}

	/**
	 * 保存服务包-服务项信息
	 * @param map
	 */
	private void saveFwbFwxx(Map<String, String> map){
		fwbFwxxDAO.deleteByFwbId(map);
		List<Map> list = new ArrayList<Map>();
		JSONArray ja = JSONObject.parseArray(map.get("FWXS"));
		JSONObject json = new JSONObject();
		String fwbid = map.get("FWBID");
		if(!ja.isEmpty()){
			Iterator<Object> it = ja.iterator();
			while (it.hasNext()) {
				json = JSONObject.parseObject(it.next().toString());
				json.put("FWBID", fwbid);
				list.add((Map)json);
			}
		}
		fwbFwxxDAO.insertBatch(list);
	}
	
	/**
	 * 停止服务包
	 * @param map
	 */
	public JSONObject stop(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		/**检查服务包是否被占用*/
		if(fwbgxDAO.validate(map)){
			ro.changeFaultState("该服务包正被签约使用中，无法停用！");
		}else{
			update(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 启动服务包
	 * @param map
	 */
	public JSONObject start(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		update(map, ro);
		return (JSONObject)JSON.toJSON(ro);
	}

	/**
	 * 根据多个id查询服务包
	 * @param map
	 * @return
	 */
	public JSONArray queryList(Map<String, String> map) {
		JSONArray jsary = new JSONArray();
		try {
			jsary = fwbDAO.queryList(map);
			
			//数据格式化
			for(int i=0;i<jsary.size();i++){
				JSONObject item = jsary.getJSONObject(i);
				item.put("FY", item.get("FY")+"元");
				item.put("CJSJ", item.getString("CJSJ").substring(0, 10));
				String FWBZT = item.getString("FWBZT");
				if("0".equals(FWBZT)){
					item.put("FWBZT", "停用");
				}else{
					item.put("FWBZT", "启用");
				}
			}
			
			JSONObject json = new JSONObject();
			json.put("FWBID", "服务包编码");
			json.put("FWBMC", "服务包名称");
			json.put("YLJGMC", "所属机构");
			json.put("YXQMC", "有效期");
			json.put("FY", "服务费用");
			json.put("FWBZT", "状态");
			json.put("CJSJ", "创建日期");
			jsary.add(0,json);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000304","根据id查询服务项失败！");
		}
		return jsary;
	}

	@Override
	public JSONArray queryAll(Map<String, String> map) {
		// TODO Auto-generated method stub
		return fwbDAO.queryAll(map);
	}
	
	/**
	 * 查询服务包基本信息和服务包服务列表信息到健康城市系统
	 * @param map
	 * @return
	 */
	public JSONArray queryFwbAndFwxxListToJkcs(Map<String, String> map){
		JSONArray fwbArr = fwbDAO.queryFwbAndFwxxListToJkcs(map);
		
		return fwbArr;
	}
	
	
}
